var express = require('express');
var router = express.Router();
var axios = require('axios');
const mysql = require('mysql');

/* GET home page. */
router.get('/index', async function (req, res, next) {
  const connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'bdzy_video',
  });
  connection.connect();
  let total = 0;
  let countSql = `SELECT count(*) FROM video where vod_name LIKE ("%${req.query.search_name}%") Limit 0,${Number(req.query.size)}`;
  connection.query(countSql, (err, data2) => {
    if (err) {
      // console.log('err2!', err);
    } else {
      // console.log('data2', JSON.stringify(data2));
      total = JSON.parse(JSON.stringify(data2))[0]['count(*)'];
    }
  });
  let sql = `SELECT * FROM video where vod_name LIKE ("%${req.query.search_name}%") Limit ${Number(req.query.page) * Number(req.query.size) - Number(req.query.size)},${Number(req.query.size)}`;
  // console.log('sql', sql);
  let json = {};
  await connection.query(sql, (err, data) => {
    if (err) {
      // console.log('err!', err);
      json = {
        ret: 5,
      };
    } else {
      // console.log('sql接过', JSON.stringify(data));
      json = {
        ret: 0,
        data: {
          list: JSON.parse(JSON.stringify(data)),
          page: Number(req.query.page),
          size: Number(req.query.size),
          total,
        },
      };
      res.send(200, json);
    }
  });
  connection.end();
});

router.get('/douyin', async function (req, res, next) {
  const connection = mysql.createConnection({
    host: '127.0.0.1',
    user: 'root',
    password: '123456',
    database: 'bdzy_video',
  });
  connection.connect();
  let total = 0;
  let countSql = `SELECT count(*) FROM video Limit 0,${Number(req.query.size)}`;
  connection.query(countSql, (err, data2) => {
    if (err) {
      // console.log('err2!', err);
    } else {
      // console.log('data2', JSON.stringify(data2));
      total = JSON.parse(JSON.stringify(data2))[0]['count(*)'];
    }
  });
  let sql = `SELECT * FROM video Limit ${Number(req.query.page) * Number(req.query.size) - Number(req.query.size)},${Number(req.query.size)}`;
  // console.log('sql', sql);
  let json = {};
  await connection.query(sql, (err, data) => {
    if (err) {
      // console.log('err!', err);
      json = {
        ret: 5,
      };
    } else {
      console.log('sql接过', JSON.parse(JSON.stringify(data))[0].vod_id);
      const vod_id = JSON.parse(JSON.stringify(data))[0].vod_id;
      axios({
        method: 'get',
        url: `https://m3u8.apibdzy.com/api.php/provide/vod/?ac=detail&ids=${vod_id}`,
      }).then((respons) => {
        // console.log('resres', res.data.list[0]);
        json = {
          ret: 0,
          data: {
            list: respons.data.list,
            page: Number(req.query.page),
            size: Number(req.query.size),
            total,
          },
        };
        res.send(200, json);
      });
    }
  });
  connection.end();
});

module.exports = router;
